﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Collections.ObjectModel;

namespace ProjectEdificio
{
    /// <summary>
    /// Lógica de interacción para FacturaWindows.xaml
    /// </summary>
    public partial class FacturaWindows : Window
    {
        private Inquilino inquilino = null;
        private BDManagment bdm = null;

        public FacturaWindows(Inquilino inquilino)
        {
            InitializeComponent();
            this.inquilino = inquilino;
            bdm = BDManagment.getInstance();
            this.labelApellidoyNombre.Content = inquilino.Apellido + ", " + inquilino.Nombre + "     DNI:" + inquilino.DNI;
            this.listViewFacture.ItemsSource = bdm.getListFacturasForID(inquilino.ID_Inquilino);
        }

        private void buttonMostrarDH_Click(object sender, RoutedEventArgs e)
        {
            if (datePickerDesde.Text != string.Empty && datePickerHasta.Text != string.Empty)
            {
                ObservableCollection<Factura> listaFacturas = new ObservableCollection<Factura>();
                foreach (Factura d in bdm.listFacturas())
                {
                    DateTime desde = DateTime.Parse(datePickerDesde.Text);
                    DateTime hasta = DateTime.Parse(datePickerHasta.Text);
                    if (d.FechaEmitida >= desde && d.FechaEmitida <= hasta)
                    {
                        listaFacturas.Add(d);
                    }
                }
                this.listViewFacture.ItemsSource = listaFacturas;
            }
        }

        private void addFacture_Click(object sender, RoutedEventArgs e)
        {
            if (bdm.addFactura(inquilino.ID_Inquilino) == 3)
            {
                MessageBox.Show("Factura Emitida!");
                this.listViewFacture.ItemsSource = bdm.getListFacturasForID(inquilino.ID_Inquilino);
            }
            else
            {
                MessageBox.Show("La factura no se emitio porque faltan agregar Gastos(Expensas o Servicios)!");
            }
        }

        private void removeFacture_Click(object sender, RoutedEventArgs e)
        {
            if (listViewFacture.SelectedItem != null)
            {
                MessageBoxButton buttons = MessageBoxButton.YesNo;
                MessageBoxImage icon = MessageBoxImage.Question;
                if (MessageBox.Show("Desea eliminar la Factura?", "Confirmación", buttons, icon) == MessageBoxResult.Yes)
                {
                    if (MessageBox.Show("Esta seguro?", "Confirmación", buttons, icon) == MessageBoxResult.Yes)
                    {
                        Factura fact = (Factura)listViewFacture.SelectedItem;
                        bdm.removeFacture(fact.ID_Factura);
                        this.listViewFacture.ItemsSource = bdm.getListFacturasForID(inquilino.ID_Inquilino);
                    }
                }
                
            }
            else
            {
                MessageBox.Show("Debe seleccionar una factura para borrarla!");
            }
        }


        private void printFacture_Click(object sender, RoutedEventArgs e)
        {
            if (listViewFacture.SelectedItem != null)
            {
                Factura f = (Factura)listViewFacture.SelectedItem;
                PrintFactura print = new PrintFactura(inquilino, f);
                print.Show();
            }
        }

        private void buttonMostrarT_Click(object sender, RoutedEventArgs e)
        {
            this.listViewFacture.ItemsSource = bdm.getListFacturasForID(inquilino.ID_Inquilino);
        }

        private void pagarFacture_Click(object sender, RoutedEventArgs e)
        {
            if (listViewFacture.SelectedItem != null)
            {
                Factura f = (Factura)listViewFacture.SelectedItem;
                if (f.Estado == "Debe")
                {
                    bdm.pagarFacture(f);
                    this.listViewFacture.ItemsSource = bdm.getListFacturasForID(inquilino.ID_Inquilino);
                }
                else
                {
                    MessageBox.Show("Esta factura ya se pago!");
                }
            }
        }
    }
}
